package com.seamtop.cuber.common.util;

import com.alibaba.fastjson.JSONObject;
import com.seamtop.cuber.common.base.DataObject;
import com.seamtop.cuber.common.entriy.CuberMessage;
import com.seamtop.cuber.common.entriy.CuberReducer;
import com.seamtop.cuber.common.entriy.HttpReturn;
import com.seamtop.cuber.common.metadata.Column;
import com.seamtop.cuber.common.metadata.TableMetaData;

import java.util.HashMap;

/**
 * Created by feng on 2015/8/9.
 */
public class CuberUtil {

    public static String transTaskJson(CuberMessage message){
        HashMap<String,CuberMessage> map = new HashMap<String,CuberMessage>();
        map.put("message",message);
        return JSONObject.toJSONString(map);
    }

    public static String getColumnName(TableMetaData tableMetaData,String match){
        HashMap<String,Column> columnMap = tableMetaData.getColumnMap();
        if(columnMap == null || columnMap.size() == 0){
            return null;
        }
        String columnName = null;
        for(String tempName : columnMap.keySet()){
            Column column = columnMap.get(tempName);
            System.out.println("column:"+column);
            System.out.println("match:"+column.getMatch());
            if(column.getMatch().equals(match)){
                columnName = column.getColumnName();
                break;
            }
        }
        return columnName;
    }

    public static CuberReducer getCuberReducer(String reducerId){
        if(StringUtil.isEmpty(reducerId)){
            return null;
        }

        CuberReducer cuberReducer = DataObject.getCuberReducerIdMap().get(reducerId);
        return cuberReducer;
    }

    public static boolean isValidateError(HttpReturn httpReturn){
        if(httpReturn == null){
            return true;
        }else if(!StringUtil.isEmpty(httpReturn.get("result")) && "0".equals(httpReturn.get("result"))){
            return false;
        }else{
            return true;
        }

    }
}
